package com.df.barcodescaning.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.df.barcodescaning.MyApplication;
import com.df.barcodescaning.R;
import com.df.barcodescaning.util.DialogUtils;

import java.util.List;

import static com.df.barcodescaning.R.string.worker;

public class ItemSelectActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "--Main--";

    private TextView tvTittle, tvContent;

    private List<String> dataList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_select);

        setTitle(getString(worker) + " " + MyApplication.getInstance().getLoggedUser().getUserName());

        findViewById(R.id.btn_select_item).setOnClickListener(this);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        tvTittle = (TextView)findViewById(R.id.tv_tittle);
        tvContent = (TextView)findViewById(R.id.tv_content);

        dataList = MyApplication.getInstance().getDataList();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_select_item:
                showSelectBarcodeDialog();
                break;
            case R.id.btn_confirm:
                confirmAndNext();
                break;
            default:
                break;
        }
    }

    private void confirmAndNext() {
        Intent intent = new Intent();
        intent.setClass(this, ScaningActivity.class);
        intent.putExtra(ScaningActivity.SCANACT_KEY_ID, tvTittle.getText().toString());
        intent.putExtra(ScaningActivity.SCANACT_KEY_TAG, tvContent.getText().toString());
        startActivity(intent);
        finish();
    }


    int selectIndex = -1;
    private void showSelectBarcodeDialog() {
        String[] array = new String[dataList.size()];
//        array = dataList.toArray(array);
        for (int i = 0; i < dataList.size(); i++) {
            String[] strArr = dataList.get(i).split(",");
            array[i] = strArr[0];
        }
        new DialogUtils(this, getString(R.string.tittle_select_barcode))
                .singleChoiceDialog(array, selectIndex,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                selectIndex = i;
                            }
                        },
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (selectIndex != -1) showSelectResult(selectIndex);
                            }
                        });
    }

    public void showSelectResult(int index) {
        findViewById(R.id.ll_result).setVisibility(View.VISIBLE);
//        Toast.makeText(this, index+"", Toast.LENGTH_SHORT).show();
        String result = dataList.get(index);
        String[] strArr = result.split(",");
//        btnSelect.setVisibility(View.GONE);
        tvTittle.setText(strArr[0]);
        if(strArr.length > 1) {
            tvContent.setVisibility(View.VISIBLE);
            tvContent.setText(result.substring(result.indexOf(",")+1));
        } else {
            tvContent.setVisibility(View.GONE);
        }
    }
}
